package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Platform;
import com.example.demo.entity.Platform;
import com.example.demo.entity.PlatformAward;
import com.example.demo.entity.PlatformMember;
import com.example.demo.entity.vo.PlatformVo;
import com.example.demo.mapper.PlatformMapper;
import com.example.demo.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lzc
 * @date 2021/11/4 19 25
 * discription
 */
@Slf4j
@Service
public class PlatformServiceImpl extends ServiceImpl<PlatformMapper, Platform> implements PlatformService {

    @Autowired
    private PlatformAwardServiceImpl platformAwardService;

    @Autowired
    private PlatformMemberService platformMemberService;

    @Autowired
    private PlatformProjectService platformProjectService;

    @Autowired
    private PlatformPaperService platformPaperService;

    @Autowired
    private ResearchService researchService;

    @Override
    public Page<PlatformVo> listPlatformsByPage(int page, int pageSize,String level) {
        QueryWrapper<Platform> queryWrapper =  new QueryWrapper<Platform>().eq(level != null,"level",level);

        Page<Platform> result = super.page(new Page<>(page, pageSize),queryWrapper);

        log.info("分页查询Platform完毕: 结果数 = {} ",result.getRecords().size());
        Page<PlatformVo> voPage=new Page<>();
        List<PlatformVo> voList=new ArrayList<>(result.getRecords().size());
        for(Platform platform : result.getRecords()){
            PlatformVo platformVo = getPlatformVoByPlatform(platform);
            voList.add(platformVo);
        }
        BeanUtils.copyProperties(result,voPage);
        voPage.setRecords(voList);
        return voPage;
    }

    @Override
    public PlatformVo getById(Integer id) {
        QueryWrapper<Platform> queryWrapper =  new QueryWrapper<Platform>().eq(id != null,"id",id);
        Platform platform = super.getOne(queryWrapper);
        return getPlatformVoByPlatform(platform);
    }

    private PlatformVo getPlatformVoByPlatform(Platform platform) {
        PlatformVo platformVo = new PlatformVo();
        platformVo.setPlatform(platform);
        platformVo.setAwardList(platformAwardService.findByPlatformId(platform.getId()));
        platformVo.setMemberList(platformMemberService.findByPlatformId(platform.getId()));
        platformVo.setPaperList(platformPaperService.findByPlatformId(platform.getId()));
        platformVo.setProjectList(platformProjectService.findByPlatformId(platform.getId()));
        platformVo.setResearchVoList(researchService.findByPlatformId(platform.getId()));

        return platformVo;
    }
}
